const {STYLE_UNIT_TYPE} = require('./const')
const {attributeOwner}=require('../config/sort')
const ordinary=(styleSheetUnitText)=>{
    const rule = styleSheetUnitText.split(/\{(.+?)\}/);
    return {
        selector: rule[0] && rule[0].trim(),
        rule: rule[1] && rule[1].split(';').reduce((pre, cur) => {
            const keyMap = cur.split(':');
            if (keyMap && keyMap.length === 2) {
                return [...pre, {
                    [keyMap[0] && keyMap[0].trim()]: {
                        text:keyMap[1] && keyMap[1].trim(),
                        ownerStaged:attributeOwner[keyMap[0] && keyMap[0].trim()]
                    }
                }]
            }
            return pre
        }, []) || []
    }
};
const ordinary2=(styleSheetUnitText)=>{
    const rule = styleSheetUnitText.split(/\{(.*)\}/);
    console.error('999',rule[1])

    return {
        ruleName:rule[0] && rule[0].trim(),
        childRule:rule[1].split(/(.+?\{([^\{\}])+?\})/).reduce((pre,cur)=>{
            if (cur.trim()) {
                console.error(7777777777777777777,cur,ordinary(cur))
                return [...pre,ordinary(cur)]
            }
            return  pre
        },[])
    }
};

const justText=(text)=>{
  return ({text})
};
module.exports={
    [STYLE_UNIT_TYPE.ORDINARY]:ordinary,
    [STYLE_UNIT_TYPE.PACE]:ordinary,
    [STYLE_UNIT_TYPE.FONT_FACE]:ordinary,
    [STYLE_UNIT_TYPE.CHARSET]:justText,
    [STYLE_UNIT_TYPE.IMPORT]:justText,
    [STYLE_UNIT_TYPE.MEDIA]:ordinary2,
    [STYLE_UNIT_TYPE.KEY_FRAMES]:ordinary2,
    [STYLE_UNIT_TYPE.COMMENT]:justText,
};
